
import api from '../../utils/api'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageBaseURL: app.imageBaseURL,
    imageResizeParams: app.imageResizeParams,
    labellist:[
      {
        id:1,
        img:'../../images/active/all.png',
        name:'全部',
        selected:true,
      }
    ],//标签列表
    todaylist:[],//今日活动
    list:[],//列表
    labelid:0,//标签id
    page:1,
    pagesize:10,
    typeflag:'_new_list',//

   
  },
  gomenuPage(e) {
    let url = e.currentTarget.dataset['navurl'];
    wx.navigateTo({
      url: url
    })

  },
  //顶部选择活动
  selectLabel(e){
    let ind = e.currentTarget.dataset.ind;
    let selected = e.currentTarget.dataset.selected;
    this.data.labellist.forEach((item,index) => {
      if(index == ind){
        item.selected = true
      }else{
        item.selected = false
      }
    })

    this.setData({
      labelid:e.currentTarget.dataset.id,labellist:this.data.labellist,page:1,list:[]
    })
    this.getToday(this.data.labelid)
    this.getActiveList(this.data.typeflag,this.data.labelid) 
  },
  //最新活动，
  onChange(e){
    let ind = e.detail.index
    let flag
    this.setData({page:1,list:[]})
    if(ind == 0){
      flag = '_new_list'
      this.getActiveList(flag,this.data.labelid)  
    }else if(ind == 1){
      flag = '_most_member_list'
      this.getActiveList(flag,this.data.labelid)  
    }else if(ind == 2){
      flag = '_min_distance_list'
      this.getActiveList(flag,this.data.labelid)  
    }else{
      flag = '_online_list'
      this.getActiveList(flag,this.data.labelid)  
    }
    this.setData({
      typeflag:flag
    })
    
  },
  //labels
  async getLabels () {
    let data={}
    data['flag'] = 'v2index'
    data['page'] =1
    data['pagesize'] = 100
    data['cityid'] =app.globalData.firstAreaId
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      result.label.forEach((item,index) => {
        item.img = this.loadImg(item.img)
        item.selected = false
      })
      var arr1 = this.data.labellist
      var arr2 = result.label

      var arr = arr1.concat(arr2);//[1,2,3,3,4,5]
      this.setData({
        labellist:arr,
        // labelid:arr[0].id
      })
    }else {
      // Toast(message)
    }
  },
  //今日活动
  async getToday (label) {
    let data={}
    data['flag'] = '_today_list'
    data['password'] ='1234543'
    data['cityName'] = app.globalData.address.city
    data['citycode'] =app.globalData.firstAreaId
    data['location[lng]'] = app.globalData.currpoint.longitude
    data['location[lat]'] = app.globalData.currpoint.latitude
    data['page'] = 1
    data['pagesize'] = this.data.pagesize
    data['label'] = label
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      result.forEach((item,index) => {
        item.imgurl[0] = this.loadImg(item.imgurl[0])
      })
      this.setData({
        todaylist:result,
      })
    
    }else {
      // Toast(message)
    }
  },
  //最新活动
  async getActiveList (flag,label) {
    let data={}
    data['flag'] = flag
    data['password'] ='1234543'
    data['cityName'] =app.globalData.address.city
    data['citycode'] =app.globalData.firstAreaId
    data['location[lng]'] = app.globalData.currpoint.longitude
    data['location[lat]'] = app.globalData.currpoint.latitude
    data['page'] = this.data.page
    data['pagesize'] = this.data.pagesize
    data['label'] = label
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      if( result && result.length>0 ){
        result.forEach((item,index) => {
          item.imgurl[0] = this.loadImg(item.imgurl[0])
        })
        let page = this.data.page+1
        let str = 'list['+(this.data.page-1)+']'
        this.setData({
          [str]:result,page
        })
      }
    
    }else {
      // Toast(message)
    }
  },
  async load(){
    await this.getLabels()
    this.getToday(this.data.labelid)
    this.getActiveList(this.data.typeflag,this.data.labelid)
  },
  //图片加载
  loadImg(img){
    let url =  api.checkurl(img)
    return url
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.load()
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.getUser()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getActiveList(this.data.typeflag,this.data.labelid)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})